package com.dandelion.admin.manager;

import com.dandelion.admin.criteria.OrderListQueryCriteria;
import com.dandelion.admin.domain.OrderDetail;
import com.dandelion.admin.domain.OrderItem;
import com.dandelion.admin.domain.OrderListItem;
import com.dandelion.admin.service.OrderService;
import com.dandelion.common.dao.auto.pojo.Order;
import com.dandelion.common.domain.ExprStatusEnum;
import com.dandelion.common.domain.OrderStatusEnum;
import com.dandelion.common.service.DictDataService;
import com.rockit.core.pojo.Pagination;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

@Service
public class OrderManager {
    @Autowired
    private OrderService orderService;

    @Autowired
    private DictDataService dictDataService;

    public List<OrderListItem> queryOrderList(OrderListQueryCriteria criteria, Pagination pagination) {
        List<OrderListItem> list = orderService.queryOrderList(criteria, pagination);
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }
        return list;
    }

    public int countOrderList(OrderListQueryCriteria criteria) {
        return orderService.countOrderList(criteria);
    }

    private OrderListItem getOrderByOrderNo(String orderNo) {
        OrderListQueryCriteria criteria = new OrderListQueryCriteria();
        criteria.setOrderNo(orderNo);
        List<OrderListItem> items = this.queryOrderList(criteria, new Pagination());
        return CollectionUtils.isEmpty(items) ? null : items.get(0);
    }

    public OrderDetail getOrderDetail(String orderNo) {
        OrderListItem order = this.getOrderByOrderNo(orderNo);
        if (order == null) {
            return null;
        }
        List<OrderItem> orderItems = orderService.getOrderItemsByOrderNo(orderNo);
        OrderDetail detail = new OrderDetail();
        detail.setItems(orderItems);
        detail.setOrder(order);
        return detail;
    }

    @Transactional
    public void updateExprStatus(Long orderId, Byte exprStatus) {
        Order order = orderService.getOrderById(orderId);
        if (order == null || order.getIsDeleted() == 1) {
            return;
        }

        OrderStatusEnum orderStatus = OrderStatusEnum.of(order.getStatus());
        if (orderStatus != OrderStatusEnum.PROCESSING) {
            return;
        }

        if (order.getStatus() >= exprStatus) {
            return;
        }

        ExprStatusEnum exprStatusEnum = ExprStatusEnum.of(exprStatus);

        if (exprStatusEnum == ExprStatusEnum.RECEIVED && order.getVerify() == 0) {
            orderService.updateExprStatusAndFinished(orderId, exprStatus);
            return;
        }

        if (exprStatusEnum == ExprStatusEnum.VERIFIED) {
            orderService.updateExprStatusAndFinished(orderId, exprStatus);
            return;
        }

        orderService.updateExprStatus(orderId, exprStatus);
    }

    @Transactional
    public void orderCancel(Long orderId) {
        Order order = orderService.getOrderById(orderId);
        if (order == null || order.getIsDeleted() == 1) {
            return;
        }

        OrderStatusEnum orderStatus = OrderStatusEnum.of(order.getStatus());
        if (orderStatus != OrderStatusEnum.PROCESSING) {
            return;
        }

        orderService.orderCancel(orderId);
    }
}
